<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button
            type="primary"
            size="small"
            @click="addPermission(1, '0')"
          >添加权限</el-button>
        </div>
        <el-table
          :data="list"
          row-key="id"
          border
        >
          <el-table-column label="名称" align="center" prop="name" />
          <el-table-column label="标识" align="center" prop="code" />
          <el-table-column label="描述" align="center" prop="description" />
          <el-table-column label="操作" align="center">
            <template #default="{row}">
              <el-button
                v-if="row.type === 1"
                size="small"
                type="text"
                @click="addPermission(2, row.id)"
              >添加权限点</el-button>
              <el-button size="small" type="text" @click="clickEdit(row.id)">查看</el-button>
              <el-button
                size="small"
                type="text"
                @click="delPermission(row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 新增/编辑的弹层 -->
      <el-dialog :visible="showDialog" :title="showTitle" @close="closeDialog">
        <!-- 表单内容 -->
        <el-form ref="form" :model="formData" :rules="rules" label-width="100px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="btnOk">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionList, addPermission, delPermission, getPermissionDetail, updatePermission } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      // 1. 存放所有的权限列表
      list: [],
      // 2. 显示/隐藏新增弹窗
      showDialog: false,
      // 3. 表单的数据对象
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 权限名称
        code: '', // 权限标识,用于将来判断
        description: '', // 权限描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      },
      // 4. 表单的校验规则对象
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑权限' : '添加权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 1. 获取所有的权限列表
    async getPermissionList() {
      const res = await getPermissionList()
      this.list = tranListToTreeData(res, '0')
      // console.log(this.list)
    },
    // 2. 点击了添加按钮
    addPermission(type, pid) {
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
    },
    // 3. 点击添加按钮
    async btnOk() {
      await this.$refs.form.validate()
      // await addPermission(this.formData)
      // this.$message.success('添加权限成功')
      // 判断是编辑还是添加
      if (this.formData.id) {
        await updatePermission(this.formData)
        this.$message.success('修改权限成功')
      } else {
        await addPermission(this.formData)
        this.$message.success('添加权限成功')
      }
      this.closeDialog()
      this.getPermissionList()
    },
    // 4. 关闭弹窗
    closeDialog() {
      // 4.2 清除校验规则
      this.$refs.form.resetFields()
      // 4.3 关闭弹窗
      this.showDialog = false
      setTimeout(() => {
        // 4.1 数据重置
        this.formData = {
          enVisible: '0', // 启用禁用, 0禁用, 1启用
          name: '', // 权限名称
          code: '', // 权限标识,用于将来判断
          description: '', // 权限描述
          type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
          pid: '' // 添加到哪个节点下
        }
      }, 300)
    },
    // 5. 删除权限
    async delPermission(id) {
      // await this.$confirm('确认删除么?', '温馨提示', { type: 'warning' })
      // await delPermission(id)
      // this.$message.success('删除成功~')
      // this.getPermissionList()
      try {
        await this.$confirm('确认删除么?', '温馨提示', { type: 'warning' })
        await delPermission(id)
        this.$message.success('删除成功~')
        this.getPermissionList()
      } catch (error) {
        console.log(error)
      }
    },
    async clickEdit(id) {
      // console.log(id)
      // 6.2 根据权限的id获取权限详情
      const res = await getPermissionDetail(id)
      // 6.3 数据回显
      // console.log(res)
      this.formData = res
      this.showDialog = true
    }
  }
}
</script>

<style>

</style>
